<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */

class Controller_Xrd_Lrdd extends Controller_Abstract implements Controller_Interface {
    
    /**
     * @var Minime_User
     */
    private $user;
    
    public function __construct($getParams, $postParams, $phpInput, $conf) {
        
        parent::__construct($getParams, $postParams, $phpInput, $conf);
    }
    
    public function execute () {
        
        // REDO This whole method
        $uri = html_entity_decode(urldecode($this->getParams['uri']));
        if(substr($uri, 0, 4) == 'http') {
            // Hack hack hack to support http: URI's as well.
            $mask = str_replace(Minime_HrefRenderer::KEY_USERNAME, "", $this->conf['url']['profile']);
            $mask = html_entity_decode($mask);
            //echo "\nmask: '" . $mask ."'";
            //echo "\nuri : '" . $uri ."'";
            $username = str_replace($mask, "", $uri);
            //echo "\nuser: '" . $username ."'\n";
        } else {
            list($username, $domain) = explode('@', self::parseJidFromAcctUri($uri));    
        }
        
        $userLoader = new Minime_User_Loader($this->conf);
        $this->user = $userLoader->getUser($username);
        if(!$this->user) {
            // Set some headers
            // Set some body
            $this->headers[404] = "404 Not Found";
            throw new Exception("User '" . $username . "' Not found!");
            return;
        }
        
        $lrddBuildArray[Xrd_Lrdd::KEY_SUBJECT] = "acct:" . $this->user->getJid();
        $lrddBuildArray[Xrd_Lrdd::KEY_ALIAS] = $this->user->getUri();
        $lrddBuildArray[Xrd_Lrdd::KEY_ATOM] = Minime_HrefRenderer::getLdrrAtomHref($username, $this->conf);
        $lrddBuildArray[Xrd_Lrdd::KEY_SALMON] = Minime_HrefRenderer::getLdrrSalmonHref($username, $this->conf);
        $lrddBuildArray[Xrd_Lrdd::KEY_MAGIC_PUBLIC_KEY] = $this->user->getEncodedPublicKey();
        $lrddBuildArray[Xrd_Lrdd::KEY_WEBFINGER_PROFILE_PAGE] = $this->user->getUri();
        
        $lrdd = Xrd_Lrdd::buildFromArray($lrddBuildArray);
        
        $this->output = $lrdd->asXML(); 
        $this->headers['Content-Type'] = "text/xml";
    }
    
    private static function parseJidFromAcctUri($encodedJID) {
        return str_replace("acct:", "", urldecode($encodedJID));
    }
}